﻿Imports System.IO
Imports System.Text
Imports System.Web
Imports System.Collections.Specialized
Imports System.Collections.ObjectModel
Imports System.Reflection
Imports System.Management.Automation

Module Module1
    Sub LoadTemplate(ByVal sTemplateName As String)
        '------------------------------------------------------------------------------------------------------------------------------------------------------
        'Read the tempplate file:
        Dim myAssembly As Assembly = Assembly.GetExecutingAssembly
        Dim myStr As StreamReader = New StreamReader(myAssembly.GetManifestResourceStream("WikiToolbox." + sTemplateName + ".txt"), Encoding.ASCII)
        Dim sTemplate As String = myStr.ReadToEnd
        '------------------------------------------------------------------------------------------------------------------------------------------------------
        Dim dataObject As New DataObject
        dataObject.SetText(sTemplate, TextDataFormat.Text)
        '------------------------------------------------------------------------------------------------------------------------------------------------------
        'Dim s As String
        'For Each s In myAssembly.GetManifestResourceNames
        'MsgBox(s)
        'Next
        Dim myStr2 As StreamReader = New StreamReader(myAssembly.GetManifestResourceStream("WikiToolbox.HTMLTemplate.txt"), Encoding.ASCII)
        Dim tHeader As String = myStr2.ReadToEnd

        Dim iStartFragment As Integer = tHeader.IndexOf("<!--StartFragment-->") + "<!--StartFragment-->".Length + 2
        Dim iEndFragment As Integer = tHeader.IndexOf("<!--EndFragment-->") + sTemplate.Length - "__LINES__".Length
        Dim iStartHtml As Integer = tHeader.IndexOf("<!DOCTYPE")
        Dim iEndHtml = tHeader.Length + sTemplate.Length - "__LINES__".Length

        tHeader = tHeader.Replace("StartHTML:0000000000", "StartHTML:" + iStartHtml.ToString("0000000000"))
        tHeader = tHeader.Replace("EndHTML:0000000000", "EndHTML:" + iEndHtml.ToString("0000000000"))

        tHeader = tHeader.Replace("StartFragment:0000000000", "StartFragment:" + iStartFragment.ToString("0000000000"))
        tHeader = tHeader.Replace("EndFragment:0000000000", "EndFragment:" + iEndFragment.ToString("0000000000"))

        tHeader = tHeader.Replace("StartSelection:0000000000", "StartSelection:" + iStartFragment.ToString("0000000000"))
        tHeader = tHeader.Replace("EndSelection:0000000000", "EndSelection:" + iEndFragment.ToString("0000000000"))

        tHeader = tHeader.Replace("__LINES__", sTemplate)
        dataObject.SetText(tHeader, TextDataFormat.Html)
        Clipboard.SetDataObject(dataObject, True)
        '------------------------------------------------------------------------------------------------------------------------------------------------------
        MsgBox("Template copied to clipboard successfully", MsgBoxStyle.Information, "Copy Template To Clipboard")
        '------------------------------------------------------------------------------------------------------------------------------------------------------
    End Sub

    Sub AppendHtmlSpan(block As String, tokenColor As String, tokenColors As NameValueCollection, ByRef lineBuilder As StringBuilder, ByRef codeBuilder As StringBuilder, ByRef currentLine As Integer)
        If (tokenColor.Equals("NewLine") Or tokenColor.Equals("LineContinuation")) Then
            If (tokenColor.Equals("LineContinuation")) Then
                codeBuilder.Append("`")
            End If
            'Not sure if this is the right translation...
            'codeBuilder.Append("<br />`r`n")
            codeBuilder.Append("<br />")

            lineBuilder.Append(currentLine.ToString("000") + "<BR />")
            currentLine += 1
        Else
            block = HttpUtility.HtmlEncode(block)
            If (block.Contains(" ")) Then
                block = block.Replace(" ", "&nbsp;")
            End If

            Dim htmlColor As String = tokenColors(tokenColor).ToString().Replace("#FF", "#")

            If (tokenColor.Equals("String")) Then
                Dim lines As String() = block.Split("`r`n")
                block = ""
                Dim multipleLines As Boolean = False
                Dim line As String
                For Each line In lines
                    If (multipleLines) Then
                        block += "<BR />`r`n"
                        lineBuilder.Append(currentLine.ToString("000") + "<BR />")
                        currentLine += 1
                    End If

                    Dim newText As String = line.TrimStart()
                    Dim i As Integer
                    For i = 1 To line.Length - newText.Length
                        newText = "&nbsp;" + newText
                    Next
                    block += newText
                    multipleLines = True
                Next
            End If

            codeBuilder.Append("<span style='color:" + htmlColor + "'>" + block + "</span>")

        End If
    End Sub

    Sub LoadPowerShellScript(ByVal sFileName As String)
        Try
            '------------------------------------------------------------------------------------------------------------------------------------------------------
            'Deffine valid colors:
            Dim tokenColors As New NameValueCollection
            tokenColors.Add("Attribute", "LightBlue")
            tokenColors.Add("Command", "Blue")
            tokenColors.Add("CommandArgument", "BlueViolet")
            tokenColors.Add("CommandParameter", "Navy")
            tokenColors.Add("Comment", "DarkGreen")
            tokenColors.Add("GroupEnd", "Black")
            tokenColors.Add("GroupStart", "Black")
            tokenColors.Add("Keyword", "DarkBlue")
            tokenColors.Add("LineContinuation", "Black")
            tokenColors.Add("LoopLabel", "DarkBlue")
            tokenColors.Add("Member", "Black")
            tokenColors.Add("NewLine", "Black")
            tokenColors.Add("Number", "Navy")
            tokenColors.Add("Operator", "DarkGray")
            tokenColors.Add("Position", "Black")
            tokenColors.Add("StatementSeparator", "Black")
            tokenColors.Add("String", "DarkRed")
            tokenColors.Add("Type", "Teal")
            tokenColors.Add("Unknown", "Black")
            tokenColors.Add("Variable", "OrangeRed")
            '------------------------------------------------------------------------------------------------------------------------------------------------------
            'Read the PowerShell script file:
            Dim sr As New StreamReader(sFileName, Encoding.ASCII)
            Dim script As String = sr.ReadToEnd
            '------------------------------------------------------------------------------------------------------------------------------------------------------
            'Parse the PowerShell script file into tokens
            Dim errors As Collection(Of PSParseError) = Nothing
            Dim tokens As Collection(Of PSToken) = Nothing
            tokens = PSParser.Tokenize(script, errors)
            '------------------------------------------------------------------------------------------------------------------------------------------------------
            Dim currentLine As Integer = 1
            Dim codeBuilder As New StringBuilder
            Dim lineBuilder As New StringBuilder

            lineBuilder.Append(currentLine.ToString("000") + "<BR />")
            currentLine += 1

            Dim iPos As Integer = 0
            Dim token As PSToken
            Dim block As String
            Dim tokenColor As String

            For Each token In tokens
                If (iPos < token.Start) Then
                    block = script.Substring(iPos, token.Start - iPos)
                    tokenColor = "Unknown"
                    AppendHtmlSpan(block, tokenColor, tokenColors, lineBuilder, codeBuilder, currentLine)
                End If

                block = script.Substring(token.Start, token.Length)
                tokenColor = token.Type.ToString
                AppendHtmlSpan(block, tokenColor, tokenColors, lineBuilder, codeBuilder, currentLine)
                iPos = token.Start + token.Length
            Next
            '------------------------------------------------------------------------------------------------------------------------------------------------------
            Dim myAssembly As Assembly = Assembly.GetExecutingAssembly
            Dim myStr As StreamReader = New StreamReader(myAssembly.GetManifestResourceStream("WikiToolbox.PowerShell.html"))
            Dim tHeader As String = myStr.ReadToEnd
            tHeader = tHeader.Replace("__LINES__", lineBuilder.ToString())

            Dim sCode As String = codeBuilder.ToString
            Dim iStartFragment As Integer = tHeader.IndexOf("<!--StartFragment-->") + "<!--StartFragment-->".Length + 2
            Dim iEndFragment As Integer = tHeader.IndexOf("<!--EndFragment-->") + sCode.Length - "__HTML__".Length
            Dim iStartHtml As Integer = tHeader.IndexOf("<!DOCTYPE")
            Dim iEndHtml = tHeader.Length + sCode.Length - "__HTML__".Length

            tHeader = tHeader.Replace("StartHTML:0000000000", "StartHTML:" + iStartHtml.ToString("0000000000"))
            tHeader = tHeader.Replace("EndHTML:0000000000", "EndHTML:" + iEndHtml.ToString("0000000000"))

            tHeader = tHeader.Replace("StartFragment:0000000000", "StartFragment:" + iStartFragment.ToString("0000000000"))
            tHeader = tHeader.Replace("EndFragment:0000000000", "EndFragment:" + iEndFragment.ToString("0000000000"))

            tHeader = tHeader.Replace("StartSelection:0000000000", "StartSelection:" + iStartFragment.ToString("0000000000"))
            tHeader = tHeader.Replace("EndSelection:0000000000", "EndSelection:" + iEndFragment.ToString("0000000000"))

            tHeader = tHeader.Replace("__HTML__", sCode)
            '------------------------------------------------------------------------------------------------------------------------------------------------------
            Dim dataObject As New DataObject
            dataObject.SetText(tHeader, TextDataFormat.Html)
            Clipboard.SetDataObject(dataObject, True)
            '------------------------------------------------------------------------------------------------------------------------------------------------------
            MsgBox("PowerShell Script copied to clipboard successfully", MsgBoxStyle.Information, "Copy Windows PowerShell Script File To Clipboard")
            '------------------------------------------------------------------------------------------------------------------------------------------------------
        Catch ex As Exception
            Throw ex
        End Try


    End Sub
End Module
